﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MM.DBUtility;
using MM.Common;
using System.Collections;

namespace WebMMS.Report
{
    public partial class RptTask : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;
                }
                if (!IsPostBack)
                {
                    BinddlPID(ref ddlPID);
                    ddlPID.Items.Insert(0, new ListItem("全部", "-1"));
                    BindddlAppID(ref ddlAppID);
                    ddlAppID.Items.Insert(0, new ListItem("全部", "-1"));
                    BindddlTaskType(ref ddlTaskType);
                    ddlTaskType.Items.Insert(0, new ListItem("全部", "-1"));

                    inputDateFrom.Value = DateTime.Now.ToString("yyyy-MM-dd");
                    inputDateTo.Value = DateTime.Now.ToString("yyyy-MM-dd");

                    AspNetPager1.CurrentPageIndex = 1;
                    GVDataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #region DDL Bind
        private void BinddlPID(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT PID,PValue FROM T_Pinfo WITH(NOLOCK) ").Tables[0];
            ddl.DataValueField = "PID";
            ddl.DataTextField = "PValue";
            ddl.DataBind();
        }
        private void BindddlAppID(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT APPID,APPValue FROM T_APPinfo WITH(NOLOCK) ").Tables[0];
            ddl.DataValueField = "APPID";
            ddl.DataTextField = "APPValue";
            ddl.DataBind();
        }
        private void BindddlTaskType(ref DropDownList ddl)
        {
            ddl.DataSource = DbHelperSql.Query("SELECT ID,Value FROM T_TaskType WITH(NOLOCK) ").Tables[0];
            ddl.DataValueField = "ID";
            ddl.DataTextField = "Value";
            ddl.DataBind();
        }
        #endregion
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #region GV info
        private void GVDataBind()
        {
            string strSQL = "SELECT a.CreateTime,b.ID AS UserID,b.IMSI+b.Mac AS UserInfo,c.PID,c.PValue,d.APPID,d.APPValue,e.Value AS TypeName FROM T_TaskRecord a,T_MobileUser b,T_Pinfo c,T_APPinfo d,T_TaskType e WITH(NOLOCK) WHERE a.MobileID=b.ID AND b.Pid=c.Pid and b.Appid=d.Appid and a.TaskType=e.ID ";
            if (!"-1".Equals(ddlPID.SelectedValue))
                strSQL += string.Format(" AND b.Pid='{0}'", ddlPID.SelectedValue);
            if (!"-1".Equals(ddlAppID.SelectedValue))
                strSQL += string.Format(" AND b.Appid='{0}'", ddlAppID.SelectedValue);
            if (!"-1".Equals(ddlTaskType.SelectedValue))
                strSQL += string.Format(" AND a.TaskType={0}", ddlTaskType.SelectedValue);
            if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                strSQL += string.Format(" AND a.CreateTime BETWEEN '{0}' AND '{1}' ", inputDateFrom.Value, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd"));

            string strOrder = " ORDER BY a.CreateTime DESC";

            AspNetPager1.RecordCount = DbHelperSql.GetSingle("SELECT COUNT(1) FROM (" + strSQL + ") AS B").ToIntOr();

            if (AspNetPager1.CurrentPageIndex == 1)
                strSQL = string.Format(strSQL.Replace("SELECT ", "SELECT TOP {0} "), AspNetPager1.PageSize) + strOrder;
            else
                strSQL = string.Format("SELECT TOP {0} * FROM ({1}) AS F WHERE rn BETWEEN {2} AND {3}", AspNetPager1.PageSize, strSQL.Replace("SELECT ", string.Format("SELECT row_number() over({0}) rn, ", strOrder)), (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize - 1);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            lbCount.Text = "记录数：" + AspNetPager1.RecordCount;
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            GVDataBind();
        }
        #endregion
    }
}